<!-- eslint-disable vue/require-prop-types -->
<template>
  <div class="conversation">
    <el-scrollbar style="height: 80vh; overflow-x: hidden">
      <div v-for="(item, index) in userList" :key="item.id + index">
        <div
          class="conv-item"
          :class="
            item.is_top == 1
              ? 'conv-item-top'
              : item.user_id == Number(props.userid)
                ? 'conv-item-self'
                : ''
          "
          @click="selectUser(item)"
          v-if="
            type == 0 ||
            (item.online == 1 && type == 1) ||
            (item.un_read > 1 && type == 2)
          "
        >
          <el-row>
            <el-col :span="5">
              <el-badge
                v-if="item.un_read > 0"
                :value="item.un_read"
                :max="9"
                class="unread-item"
              />
              <el-avatar
                :size="55"
                :src="item.head_img"
                style="margin-top: 10px"
              >
                <img :src="item.head_img" alt="" />
              </el-avatar>
              <el-badge
                v-if="item.online == 1"
                is-dot
                class="online-item"
                type="success"
              />
            </el-col>
            <el-col :span="13">
              <div class="email">
                {{ item.email }}
              </div>
              <div class="name">{{ item.username }}</div>
              <el-tag
                v-if="item.is_top == 1"
                class="top-tag"
                type="warning"
                size="small"
              >
                顶
              </el-tag>
              <div class="msg">{{ item.last_msg.content }}</div>
            </el-col>
            <el-col :span="6">
              <div class="time">{{ item.updated_at_str }}</div>
            </el-col>
          </el-row>
        </div>
      </div>
    </el-scrollbar>
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from "vue";
import { type GroupItem } from "@/utils/socket";
const props = defineProps({
  userid: {
    type: String,
    default: "",
  },
  type: {
    type: Number,
    default: 0, //0:全部 1:在线 2:未读
  },
  userList: {
    type: Array as () => GroupItem[],
    default: () => [],
  },
});

const emit = defineEmits(["clickItem", "handleCommand"]);

const selectUser = (item: any) => {
  emit("clickItem", item);
};

const handleCommand = (item: any) => {
  emit("handleCommand", item);
};
</script>
<style>
.el-scrollbar__wrap {
  overflow-x: hidden !important;
}

.conv-item {
  background-color: #fff;
  padding: 10px;
  margin: 10px 0;
  border-radius: 8px;
}

.conv-item-self {
  background-color: #dde9f1;
}

.conv-item-top {
  background-color: #fffcf3;
}

.unread-item {
  position: absolute;
  top: -5px;
  left: 40px;
}

.online-item {
  /* padding: 10px; */
  position: absolute;
  left: 40px;
  bottom: -10px;
}

.online-item .is-dot {
  width: 15px;
  height: 15px;
}
.email {
  width: 150px;
  margin-left: 10px;
  margin-top: 1px;
  font-size: 14px;
  margin-bottom: 2px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.name {
  width: 150px;
  margin-left: 10px;
  margin-top: 1px;
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 3px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.top-tag {
  position: absolute;
  right: 80px;
  top: 5px;
}

.msg {
  width: 100%;
  margin-left: 10px;
  margin-top: 5px;
  font-size: 14px;
  color: #555;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.time {
  margin-top: 25px;
  font-size: 14px;
  color: #999;
}

.operation {
  position: absolute;
  right: 5px;
  top: 5px;
}
</style>
